package com.example.administrator.kaohedemo.Adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.administrator.kaohedemo.Action.SeeClassRecordAction;
import com.example.administrator.kaohedemo.Model.SeeClassRecordAdapterBean;
import com.example.administrator.kaohedemo.Model.SeeClassRecordBean;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Utils.Utils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/24.
 */
public class SeeClassRecordAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private final SeeClassRecordAction action;
    private Context context;
    private List<SeeClassRecordAdapterBean> mDatas=new ArrayList<SeeClassRecordAdapterBean>();
    private static final int HEADITEMTYPE=1;
    private static final int DEFULTITEMTYPE=2;

    public SeeClassRecordAdapter(Context context) {
        action=SeeClassRecordAction.getInstense();
        this.context = context;
    }

    public void setList(List<SeeClassRecordBean.Mbody.Mmodules> list) {
        for (int i=0;i<list.size();i++){
            SeeClassRecordAdapterBean bean1=new SeeClassRecordAdapterBean();
            bean1.setModule_id(list.get(i).module_id);
            bean1.setModule_name(list.get(i).module_name);
            bean1.setMore(list.get(i).more);
            mDatas.add(bean1);
            SeeClassRecordAdapterBean bean2=new SeeClassRecordAdapterBean();
            bean2.setModule_id("");
            bean2.setModule_name("");
            bean2.setMore("");
            mDatas.add(bean2);
            List<SeeClassRecordBean.Mbody.Mmodules.Mcourses> courses=list.get(i).courses;
            for (int j=0;j<courses.size();j++){
                SeeClassRecordAdapterBean bean3=new SeeClassRecordAdapterBean();
                bean3.setModule_id(list.get(i).module_id);
                bean3.setModule_name(list.get(i).module_name);
                bean3.setMore(list.get(i).more);
                bean3.setRecord(courses.get(j).record);
                bean3.setCourse_img(courses.get(j).course_img);
                bean3.setCourse_title(courses.get(j).course_title);
                bean3.setCourses_id(courses.get(j).courses_id);
                mDatas.add(bean3);
            }
            if (courses.size()%2!=0){
                SeeClassRecordAdapterBean bean4=new SeeClassRecordAdapterBean();
                bean4.setModule_id("");
                bean4.setModule_name("");
                bean4.setMore("");
                bean4.setRecord("");
                bean4.setCourse_img("");
                bean4.setCourse_title("");
                bean4.setCourses_id("");
                mDatas.add(bean4);
            }
        }
        notifyDataSetChanged();
    }

    @Override
    public int getItemViewType(int position) {
        String courses_id=mDatas.get(position).getCourses_id();
        if (TextUtils.isEmpty(courses_id)){
            return HEADITEMTYPE;
        }else {
            return DEFULTITEMTYPE;
        }
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder=null;
        if (viewType==HEADITEMTYPE){
            View view= LayoutInflater.from(context).inflate(R.layout.adapter_headitem,parent,false);
            holder=new headItemViewHolder(view);
        }else {
            View view= LayoutInflater.from(context).inflate(R.layout.adapter_defultitem,parent,false);
            holder=new defaultItenViewHolder(view);
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SeeClassRecordAdapterBean bean=mDatas.get(position);
        if (holder instanceof headItemViewHolder){
            ((headItemViewHolder)holder).adapter_seeclassrecord_head.setText(bean.getModule_name());
        }else if (holder instanceof defaultItenViewHolder){
            ((defaultItenViewHolder)holder).nt_content_title.setText(bean.getCourse_title());
            if (!bean.getRecord().equals("0")) {
                ((defaultItenViewHolder) holder).nt_process_record.setVisibility(View.VISIBLE);
                ((defaultItenViewHolder) holder).nt_process_record.setText("已观看:"+Utils.getTimeFromSum(bean.getRecord()));
            }else {
                ((defaultItenViewHolder) holder).nt_process_record.setVisibility(View.INVISIBLE);
            }
        }
    }

    @Override
    public int getItemCount() {
        return mDatas==null?0:mDatas.size();
    }

    public class headItemViewHolder extends RecyclerView.ViewHolder{

        TextView adapter_seeclassrecord_head;
        public headItemViewHolder(View itemView) {
            super(itemView);
            adapter_seeclassrecord_head= (TextView) itemView.findViewById(R.id.adapter_seeclassrecord_head);
        }
    }

    public class defaultItenViewHolder extends RecyclerView.ViewHolder{

        ImageView nt_content_iv;
        TextView nt_process_record;
        TextView nt_content_title;
        public defaultItenViewHolder(View itemView) {
            super(itemView);
            nt_content_iv= (ImageView) itemView.findViewById(R.id.nt_content_iv);
            nt_process_record= (TextView) itemView.findViewById(R.id.nt_process_record);
            nt_content_title= (TextView) itemView.findViewById(R.id.nt_content_title);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SeeClassRecordAdapterBean bean=mDatas.get(getLayoutPosition());
                    if (!TextUtils.isEmpty(bean.getCourses_id())){
                        action.SendRecordClick(bean);
                    }
                }
            });
        }
    }
}
